跳到主要内容

使用magisk的简单的改机实现

· 阅读需 2 分钟
老陈

想要在安卓中改机, 最核心的是我们要做到改变几个系统属性值(prop)

我们在安装了magisk后, magisk给我们提供了resetprop的二进制文件

执行如下shell代码, 我们将机型改为红米 k60 pro

resetprop ro.product.manufacturer Xiaomi
resetprop ro.product.brand Redmi
resetprop ro.product.model 22127RK46C
resetprop ro.product.device socrates
resetprop ro.build.product socrates

我们也可以使用Yyds.Auto中的shell代码执行以上命令

执行以上代码后, 我们可以打开设备信息应用进行测试, 查看是否变更机型成功, 这时候我们发现, 改机是没有变化的

那是因为我们的安卓进程都是从克隆出来的, 系统属性已经被读取过了, 我们需要软重启所有安卓进程, 重新读取即可.

killall zygote

当然我们可以使用xposed等方法进行局部刷新修改, 注入以下java代码进行刷新

// yydsxx.com
public class ReflectUtil {
static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);

Field modifiersField = Field.class.getDeclaredField("accessFlags");
modifiersField.setAccessible(true);
int originModifier = field.getModifiers();
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
modifiersField.setInt(field, originModifier);
}
}

public class DeviceUtil {
public static String sysProperty(String key, String defValue) {
String res = null;
try {
@SuppressLint("PrivateApi") Class<?> clazz = Class.forName("android.os.SystemProperties");
Method method = clazz.getMethod("get", new Class<?>[]{String.class, String.class});
res = (String) method.invoke(clazz, new Object[]{key, defValue});
if (res == null || res.isEmpty()) {
return defValue;
}
} catch (Exception e) {
LogUtil.print("System property invoke error: " + e);
}
return res;
}
}

ReflectUtil.setFinalStatic(Build::class.java.getField("MANUFACTURER"), DeviceUtil.sysProperty("ro.product.manufacturer", Build.MANUFACTURER))
ReflectUtil.setFinalStatic(Build::class.java.getField("BRAND"), DeviceUtil.sysProperty("ro.product.brand", Build.BRAND))
ReflectUtil.setFinalStatic(Build::class.java.getField("MODEL"), DeviceUtil.sysProperty("ro.product.model", Build.MODEL))
ReflectUtil.setFinalStatic(Build::class.java.getField("DEVICE"), DeviceUtil.sysProperty("ro.product.device", Build.DEVICE))
ReflectUtil.setFinalStatic(Build::class.java.getField("PRODUCT"), DeviceUtil.sysProperty("ro.product.name", Build.PRODUCT))
ReflectUtil.setFinalStatic(Build::class.java.getField("FINGERPRINT"), DeviceUtil.sysProperty("ro.build.fingerprint", Build.FINGERPRINT))
ReflectUtil.setFinalStatic(Build.VERSION::class.java.getField("SDK_INT"), DeviceUtil.sysProperty("ro.build.version.sdk", Build.VERSION.SDK_INT.toString()).toInt())
ReflectUtil.setFinalStatic(Build.VERSION::class.java.getField("INCREMENTAL"), DeviceUtil.sysProperty("ro.build.version.incremental", Build.VERSION.INCREMENTAL))
ReflectUtil.setFinalStatic(Build.VERSION::class.java.getField("RELEASE"), DeviceUtil.sysProperty("ro.build.version.release", Build.VERSION.RELEASE))
ReflectUtil.setFinalStatic(Build.VERSION::class.java.getField("SECURITY_PATCH"), DeviceUtil.sysProperty("ro.build.version.security_patch", Build.VERSION.SECURITY_PATCH))